package tools

import (
	"SSO/config"
	"fmt"
	"github.com/gomodule/redigo/redis"
	"time"
)

var pool *redis.Pool

func initRedisPool() {

	pool = &redis.Pool{
		MaxIdle:     20,
		MaxActive:   5,
		IdleTimeout: 180 * time.Second,
		Dial: func() (redis.Conn, error) {
			c, err := redis.Dial("tcp", fmt.Sprintf("%s:%d", config.GetConf().Redis.Host, config.GetConf().Redis.Port))
			if err != nil {
				return nil, err
			}
			// 选择db
			_, _ = c.Do("SELECT", 0)
			return c, nil
		},
	}
}

func GetRedisClient() *redis.Pool {
	if pool == nil {
		initRedisPool()
	}
	return pool;
}

func CloseRedisPool() {
	_ = pool.Close();
}